/* 登陆模块 */
import { login } from '@/api/api.js'
import {getUserId, getUserInfo, getweixinUserInfo} from "@/api/homePage";
import Config from "@/config";
import {getUserinfoModify} from "@/api/qxlj";
import {getSystemValueByKey} from "@/api/mall";
export default {
	state: {
		hasLogin:uni.getStorageSync('islogin') || {}, //是否登录
		openid: null, // openid
		cookie: null, // cookie
		userInfo: {
			Id:1
		}, // 用户信息
		userMemberTags: [],
		token: "",
		isIphoneX:false,
		// 位置选择相关
		locationList: [],
		selectedLocation: {
			title: '全部景区',
			value: 0
		},
		homeHeavyLoad: false
	},
	mutations: {
		setLogin(state, provider) {
			if(!state.hasLogin) {
				state.hasLogin = true
				uni.setStorageSync('isLogin', true)
			}
			state.loginProvider = provider
		},
		setLogout(state) {
			state.hasLogin = false
			state.openid = null
			if(!state.isToLoginPage) {
				state.isToLoginPage = true
				uni.showModal({
					content: '登录过期需是否登录',
					cancelText:'稍后登录',
					confirmText:'立即登录',
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/login/login',
								fail:(rej)=> {
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		},
		setOpenid(state, openid) {
			state.openid = openid
		},
		setUserInfo(state,info) {
			state.userInfo = info
			uni.setStorageSync('userInfo', state.userInfo);
		},
		setToken(state,token) {
			state.token = token
			uni.setStorageSync('token', state.token);
		},
		setCookie(state,cookie) {
			state.cookie = cookie
			uni.setStorageSync('cookie', cookie);
		},
		setIsIphoneX(state,data) {
			state.isIphoneX = data
		},
		// 设置选择的位置
		setSelectedLocation(state, location) {
			state.selectedLocation = location
			state.homeHeavyLoad = true
			uni.setStorageSync('selectedLocation', location);
		},
		// 设置选择的位置
		setSelectedLocationHome(state, location) {
			state.selectedLocation = location
			state.homeHeavyLoad = false
			uni.setStorageSync('selectedLocation', location);
		},
		setLocationList(state, data){
			state.locationList = data
		}
	},
	actions: {
		wxLogin({ commit, dispatch, state },result) {
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					uni.getUserInfo({
						provider: 'weixin',
						success: function(infoRes) {
							getweixinUserInfo({
								appid: Config.appid,
								userInfo: infoRes.userInfo,
								encryptedData: infoRes.encryptedData,
								iv: infoRes.iv,
								rescode: loginRes.code
							}).then(res =>{
								const { Data } = res
								commit("setToken", Data.user_token)
								dispatch("getUserInfo", Data.user_token)
							})
						}
					})

				},
				fail: (err) => {
					console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err)
				}
			})
		},
		getUserInfo({commit, state}, result) {
			getUserId({
				Token: result
			}).then(res =>{
				console.log(res)
				commit("setUserInfo", res.Data)
			})
			// uni.getUserInfo({
			// 	provider: state.loginProvider,
			// 	success: (result) => {
			// 		console.log('getUserInfo success', result);
			// 	},
			// 	fail: (error) => {
			// 		console.log('getUserInfo fail', error);
			// 		let content = error.errMsg;
			// 		if (~content.indexOf('uni.login')) {
			// 			content = '请在登录页面完成登录操作';
			// 		}
			// 		uni.showModal({
			// 			title: '获取用户信息失败',
			// 			content: '错误原因' + content,
			// 			showCancel: false
			// 		});
			// 	}
			// });
		},

		getRegionalConfig({commit, state}, result){
			console.log("获取区域数据")
			getSystemValueByKey("AREA_CONFIG").then(res => {
				if (res.Data) {
					try {
						const regionalData = JSON.parse(res.Data)
						if (Array.isArray(regionalData) && regionalData.length > 0) {
							commit("setLocationList", regionalData)
						}
					} catch (e) {
						console.error('解析区域数据失败', e)
					}
				}
			}).catch(err => {
				console.error('获取区域数据失败', err)
			})
		},

		mpGetUserInfo({ commit, state }, result) {
			return new Promise((resolve,reject)=>{
				if (result.detail.errMsg !== 'getUserInfo:ok') {
					uni.showModal({
						title: '获取用户信息失败',
						content: '错误原因' + result.detail.errMsg,
						showCancel: false
					});
					reject(result.detail.errMsg)
					return;
				}
				commit('setHasUserInfo')
				let userInfo = {
					headerimg: result.detail.userInfo.avatarUrl,
					nickname: result.detail.userInfo.nickName,
				}
				commit('setUserInfo',userInfo)
				// state.userInfo = result.detail.userInfo;
				uni.setStorageSync('userInfo', userInfo);
				console.log(result.detail.userInfo)
				resolve(result.detail.userInfo)
			})
		},

		modifyUserInformation({ commit, dispatch, state }, result){
			getUserinfoModify({
				Name: result.name,
				Sex: result.sexID,
				HeadImg: result.imgList,
				Birthday: result.labelValue,
			}).then(res =>{
				uni.showToast({
					title: '修改成功',
					icon: 'success',
					duration: 2000
				});
				dispatch("getUserInfo", state.token)
				setTimeout(()=>{
					uni.switchTab({
						url: '/pages/my/index'
					});
				},2000)
			}).catch(()=>{
				uni.showToast({
					title: '修改失败',
					icon: 'error',
					duration: 2000
				});
			})

		}
	}
}
